package com.xyj.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.xyj.dao.base.BaseDaoI;
import com.xyj.pageModel.User;
import com.xyj.util.Encrypt;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xyj.dao.UserDaoI;
import com.xyj.domain.TUser;
import com.xyj.service.UserServiceI;

@Service("userService")
public class UserServiceImpl implements UserServiceI {

	public  static Logger logger = Logger.getLogger(UserServiceImpl.class);
	
	private BaseDaoI<TUser> userDao;
	public BaseDaoI<TUser> getUserDao() {
		return userDao;
	}
	@Autowired
	public void setUserDao(UserDaoI userDao) {
		this.userDao = userDao;
	}

	@Override
	public Serializable saveUser(TUser user) {
		return	userDao.save(user);
	}

    @Override
    public TUser login(User user) {
//        return  userDao.get("from com.xyj.domain.TUser t where t.name =? and  t.pwd = ?",new String[]{user.getName(),Encrypt.e(user.getPwd())});
        Map<String ,Object> params = new HashMap<>();
        params.put("name",user.getName());
        params.put("pwd",Encrypt.e(user.getPwd()));
        return  userDao.get("from com.xyj.domain.TUser t where t.name = :name and  t.pwd = :pwd ",params);
    }

    @Override
    public void saveUser(User user) {
        TUser target = new TUser();
        BeanUtils.copyProperties(user,target);
        target.setPwd(Encrypt.e(user.getPwd()));
        target.setTid(UUID.randomUUID().toString());
        target.setCreatedatetime(new Timestamp(new Date().getTime()));
        userDao.save(target);
    }

}
